#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件        :tools.py
@说明        :
@时间        :2024/01/22 15:24:54
@作者        :梁文豪
@版本        :1.0
'''


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog
from PyQt5.QtGui import QIcon
import os



def OpenDir(self):
    dir = QFileDialog.getExistingDirectory(None, "选择文件夹", "")
    if not dir:
        print("取消")
    else:
        print(dir)
        self.lbOpenDir.setText(dir)

# value=self.cmbType.currentText()
# print(value)


def SavePath(self):
    dir = QFileDialog.getExistingDirectory(None, "选择文件夹", "")
    if not dir:
        print("取消")
    else:
        print(dir)
        self.lbSavePath.setText(dir)
        
def ConvertStart(self):
    print("开始转换")
    oldDir=self.lbOpenDir.text()
    newDir=self.lbSavePath.text()
    oldExp=self.cmbType.currentText()
    newExp=self.cmbType_2.currentText()
    get_file_name(oldDir, newDir,oldExp,newExp)


def get_file_name(oldDir, newDir,oldExp,newExp):
    for root, dirs, files in os.walk(oldDir):  # 获取当前文件夹的信息
        for file in files:                       # 扫描所有文件
            if os.path.splitext(file)[1] == oldExp:  # 提取出所有后缀名为md的文件
                os.chdir(root)
                # 普通转换
                # print("转换开始：" + "pandoc " + file + " -o " +
                #       newDir+"/" +os.path.splitext(file)[0] + newExp)
                # # 使用os.system调用pandoc进行格式转化
                # os.system("pandoc " + file + " -o " + newDir +"/"+
                #           os.path.splitext(file)[0] + newExp)
                
                current_path = os.path.abspath(os.path.dirname(__file__))
                
                # 模块转换
                print("转换开始：" + "pandoc " + file + " -o " +
                      newDir+"/" +os.path.splitext(file)[0] + newExp+" --template="+current_path+"\\"+"template.tex --pdf-engine=xelatex")
                # 使用os.system调用pandoc进行格式转化
                os.system("pandoc " + file + " -o " + newDir +"/"+
                          os.path.splitext(file)[0] + newExp+" --template="+current_path+"\\"+"template.tex --pdf-engine=xelatex")
                print("转换完成...")


'''
# 启动包路径

import tools
import sys

'''


'''
        # 添加桌面事件
        self.BtnOpenDir.clicked.connect(lambda:tools.OpenDir(self))
        self.BtnSavePath.clicked.connect(lambda:tools.SavePath(self))
        self.cmbType.addItems([".md"])
        self.cmbType_2.addItems([".pdf"])
        

'''

'''
# 启动程序

if __name__ == "__main__":
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    global app
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    
'''
